package bookmanage;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.SpringLayout;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class BookInsert extends JInternalFrame {
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JTextField b_title;
	private JTextField b_author;
	private JTextField b_publisher;
	private JComboBox code1;
	private JComboBox code2;
	private JButton btnNewButton;
	private JTextField b_publishdate;
	private JDatePickerImpl datePicker;
	private SpringLayout springLayout;
	
	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
	Connection con;
	
	public BookInsert(Connection con) {
		super("도서 등록",true,true,false,true);
		this.con=con;
		setResizable(false);
		setBounds(100, 100, 290, 275);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getLabel_2());
		getContentPane().add(getLabel_3());
		getContentPane().add(getLabel_4());
		getContentPane().add(getB_title());
		getContentPane().add(getB_author());
		getContentPane().add(getB_publisher());
		getContentPane().add(getCode1());
		getContentPane().add(getCode2());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getB_publishdate());
		getContentPane().add(getDatePicker());
		selectBig();
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("책 제목");
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("저자");
			label.setBounds(12, 40, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("출판사");
			label_1.setBounds(12, 70, 57, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("출판일");
			label_2.setBounds(12, 100, 57, 15);
		}
		return label_2;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("대분류");
			label_3.setBounds(12, 130, 57, 15);
		}
		return label_3;
	}
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("소분류");
			label_4.setBounds(12, 160, 57, 15);
		}
		return label_4;
	}
	private JTextField getB_title() {
		if (b_title == null) {
			b_title = new JTextField();
			b_title.setBounds(81, 7, 170, 24);
			b_title.setColumns(10);
		}
		return b_title;
	}
	private JTextField getB_author() {
		if (b_author == null) {
			b_author = new JTextField();
			b_author.setColumns(10);
			b_author.setBounds(81, 37, 170, 24);
		}
		return b_author;
	}
	private JTextField getB_publisher() {
		if (b_publisher == null) {
			b_publisher = new JTextField();
			b_publisher.setColumns(10);
			b_publisher.setBounds(81, 67, 170, 24);
		}
		return b_publisher;
	}
	private JComboBox getCode1() {
		if (code1 == null) {
			code1 = new JComboBox();
			code1.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					String big = (String)code1.getSelectedItem();
					if(!big.equals("선택")){
						try {
							code2.removeAllItems();
							code2.addItem("선택");
							String sql="select small_name from codetbl where big_name=? and small_name is not null";
							PreparedStatement psmt = con.prepareStatement(sql);
							psmt.setString(1, big);
							ResultSet rs = psmt.executeQuery();
							while(rs.next()){
								code2.addItem(rs.getString(1));
							}
						} catch (Exception e2) {
							e2.printStackTrace();
						}
					}
				}
			});
			code1.addItem("선택");
			code1.setBounds(81, 127, 116, 23);
		}
		return code1;
	}
	private JComboBox getCode2() {
		if (code2 == null) {
			code2 = new JComboBox();
			code2.addItem("선택");
			code2.setBounds(81, 157, 116, 23);
		}
		return code2;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("등록");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String title = b_title.getText().trim();
					String author = b_author.getText().trim();
					String publisher = b_publisher.getText().trim();
					String publishdate = b_publishdate.getText().trim();
					String big = (String)code1.getSelectedItem();
					String small = (String)code2.getSelectedItem();
					
					if(big.equals("선택")) big="";
					if(small.equals("선택")) small="";
					
					if(title.equals("")){
						JOptionPane.showMessageDialog(null, "제목을 입력해주세요!");
						b_title.requestFocus();
						return;
					}
					if(author.equals("")){
						JOptionPane.showMessageDialog(null, "저자를 입력해주세요!");
						b_author.requestFocus();
						return;
					}
					if(publisher.equals("")){
						JOptionPane.showMessageDialog(null, "출판사를 입력해주세요!");
						b_publisher.requestFocus();
						return;
					}
					if(big.equals("")){
						JOptionPane.showMessageDialog(null, "대분류를 선택 해주세요!");
						return;
					}
					
					try {
						String sql="insert into bookinfo values(seq_book_no.nextval,?,?,?,?,?,?)";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setString(1, title);
						psmt.setString(2, author);
						psmt.setString(3, publisher);
						psmt.setString(4, publishdate);
						psmt.setString(5, big);
						psmt.setString(6, small);
						int rs = psmt.executeUpdate();
						if(rs==1){
							JOptionPane.showMessageDialog(null, "책 정보가 등록되었습니다!");
							b_title.setText("");
							b_author.setText("");
							b_publisher.setText("");
							b_publishdate.setText("");
							code1.setSelectedIndex(0);
							code2.setSelectedIndex(0);
						}
					} catch (Exception e2) {
						e2.printStackTrace();
					}
					
				}
			});
			btnNewButton.setFont(new Font("굴림", Font.BOLD, 15));
			btnNewButton.setBounds(102, 201, 95, 30);
		}
		return btnNewButton;
	}
	private JTextField getB_publishdate() {
		if (b_publishdate == null) {
			b_publishdate = new JTextField();
			b_publishdate.setEditable(false);
			b_publishdate.setBounds(81, 97, 100, 24);
			b_publishdate.setColumns(10);
		}
		return b_publishdate;
	}
	private JDatePickerImpl getDatePicker(){
		if (datePicker == null) {
			UtilDateModel uModel = new UtilDateModel();
			JDatePanelImpl datePanel = new JDatePanelImpl(uModel);
			datePicker = new JDatePickerImpl(datePanel);
			datePicker.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					b_publishdate.setText(sd.format((Date)datePicker.getModel().getValue()));
				}
			});
			datePicker.setBorder(null);
			springLayout = (SpringLayout) datePicker.getLayout();
			//datePicker.setBackground(Color.WHITE);
			datePicker.getJFormattedTextField().setVisible(false);
			datePicker.setSize(45, 27);
			datePicker.setLocation(180, 93);
			
		}
		return datePicker;
	}
	
	public void selectBig(){
		try {
			String sql="select big_name from codetbl where big_name!='강좌' and small_name is null";
			PreparedStatement psmt = con.prepareStatement(sql);
			ResultSet rs = psmt.executeQuery();
			while(rs.next()){
				code1.addItem(rs.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
